<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/1/1
 * Time: 17:58
 */
namespace common\sources\read\buypepperrecord;

use \common\models\craftsman\BuyPepperRecord;
use yii\data\Pagination;

class BuyPepperRecordAccess implements IBuyPepperRecordAccess
{
	// TODO:: 已弃用 【管理员调整明细位置】
    public function getList($data)
    {
        $query = BuyPepperRecord::find()
            ->alias('b')
            ->select("b.task_order_num,b.expend,b.income,b.balance,b.remark,b.update_time,b.img_url,b.action_type,b.order_id,b.create_time,
            u.phone,
            a.username")
            ->leftJoin('admin_user a','a.id = b.operatorId')
            ->leftJoin('buyuser u','u.id = b.buy_id');
        if (1 == $data['search_type']) {//1-手机，2-操作人，3-主任务编号，4-子任务编号，5-支付编号
            $query = $query->andWhere(['u.phone' => $data['search_value']]);
        }
        if (2 == $data['search_type']) {//1-手机，2-操作人，3-主任务编号，4-子任务编号，5-支付编号
            $query = $query->andWhere(['a.phone' => $data['search_value']]);
        }
        if (3 == $data['search_type']) {//1-手机，2-操作人，3-主任务编号，4-子任务编号，5-支付编号
            $query = $query->andWhere(['b.task_id' => $data['search_value']]);
        }
        if (4 == $data['search_type']) {//1-手机，2-操作人，3-主任务编号，4-子任务编号，5-支付编号
            $query = $query->andWhere(['b.task_id' => $data['search_value']]);
        }
        if (5 == $data['search_type']) {//1-手机，2-操作人，3-主任务编号，4-子任务编号，5-支付编号
            $query = $query->andWhere(['b.order_id' => $data['search_value']]);
        }
        if (1 == $data['action_role']) {//操作人 1-系统 2-结算 3-非结算
            $query = $query->andWhere(['a.role' => 1]);
        }
        if (2 == $data['action_role']) {//操作人 1-系统 2-结算 3-非结算
            $query = $query->andWhere(['a.role' => 2]);
        }
        if (2 == $data['action_role']) {//操作人 1-系统 2-结算 3-非结算
            $query = $query->andWhere("a.role != 2");
        }
        if (!empty($data['begin_time'])) {
            $query = $query->andWhere([
                '>',
                'create_time',
                $data['begin_time']
            ]);
        }
        if (!empty($data['end_time'])) {
            $query = $query->andWhere([
                '<',
                'create_time',
                $data['end_time']
            ]);
        }
        $list = $query->asArray()->all();
        return $list;
    }

	/**
	 * newGetList 用于管理员调整明细
	 * @param        $where
	 * @param        $data
	 * @param string $orderBy
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/15
	 * @return mixed
	 */
	public function newGetList($where, $data, $orderBy=' create_time desc') {
		$query = BuyPepperRecord::find()
			->select("buy_id,task_id,task_order_num,expend,income,balance,remark,update_time,img_url,type,order_id,create_time,operatorId")
			->where($where)
			->orderBy($orderBy);
		// 分页查询
		$page            = new Pagination(['totalCount' => $query->count(), 'pageSize' => $data['limit'], 'pageParam' => 'page_num']);
		$result['list']  = $query->offset($page->offset)->limit($page->limit)->asArray()->all();
		$result['count'] = $page->totalCount;
		return $result;
	}


    public function UnusualBankList() {
        $sql = "SELECT id,buy_id,balance FROM buy_pepper_record WHERE create_time IN (SELECT MAX(create_time) FROM `buy_pepper_record` GROUP BY buy_id)";
        $list = \Yii::$app->db->createCommand($sql)->queryAll();
        return $list;
    }

    public function getPepperCout($where)
    {
        return BuyPepperRecord::find()
                ->where($where)
                ->count();
    }

    public function getPepperList($data,$where)
    {
        return BuyPepperRecord::find()
                ->select("id,buy_id,task_order_num,task_order_id,expend,income,balance,freeze_change,freeze_balance,tran_num,remark,img_url,operatorId,action_type,create_time")
                ->where($where)
                ->orderBy(" id  desc")
                ->offset(($data['page_num'] -1) * $data['limit'])
                ->limit($data['limit'])
                ->asArray()
                ->all();
    }


	/**
	 * getPageList 用于 用户资金明细
	 * @param        $where
	 * @param string $orderBy
	 * @param int    $limit
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/10
	 * @return mixed
	 */
    public function getPageList($where, $orderBy = "create_time desc", $limit = 10) {
	    $query = BuyPepperRecord::find()
		    ->alias('a')
		    ->select("u.phone,a.id,a.buy_id,a.task_order_num,a.task_order_id,a.expend,a.income,a.balance,a.tran_num,a.remark,a.img_url,a.operatorId,a.action_type,a.create_time")
		    ->leftJoin('buyuser u','u.id = a.buy_id')
		    ->where($where)
		    ->orderBy($orderBy);

	    $page            = new Pagination(['totalCount' => $query->count(), 'pageSize' => $limit]);
	    $result['list']  = $query->offset($page->offset)->limit($page->limit)->asArray()->all();
	    $result['count'] = $page->totalCount;
	    return $result;
    }
}